package com.pro.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pro.dao.impl.NewsDaoImpl;
import com.pro.dao.impl.NewsTypeDaoImpl;
import com.pro.entity.NewsEntity;
import com.pro.util.BaseServlet;
import com.pro.util.DateUtil;
import com.pro.util.Result;

/**
 * Servlet implementation class NewsAdminServlet
 */
public class NewsAdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
	NewsDaoImpl nDaoImpl = new NewsDaoImpl();
	NewsTypeDaoImpl nTypeDaoImpl = new NewsTypeDaoImpl();
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	//实现分页 并且根据条件进行查询
	public void newsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Integer page = Integer.parseInt(request.getParameter("page"));
		Integer limit = Integer.parseInt(request.getParameter("limit"));
		
		String nid = request.getParameter("nid");
		String showHome = request.getParameter("showHome");
		String beginDate = request.getParameter("beginDate");
		String endDate = request.getParameter("endDate");
		System.out.println(beginDate);
		ArrayList<NewsEntity> aList = nDaoImpl.newsAdminList(page,limit,nid,showHome,beginDate,endDate);
		ArrayList<NewsEntity> aList1 = nDaoImpl.newsAdminList(null,null,nid,showHome,beginDate,endDate);
		HttpSession session = request.getSession();
		session.setAttribute("newsTypeList", nTypeDaoImpl.newsType());
		
		PrintWriter out=response.getWriter();
		out.write(com.pro.util.ResultToLayui.toClient(0, "", aList1.size(), aList));
		out.flush();
		out.close();
	}
	
	public void newsUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  int id= Integer.parseInt(request.getParameter("newsId"));
		  String newsTitle = request.getParameter("newsTitle");
		  String newsContent = request.getParameter("newsContent");
		  int nid = Integer.parseInt(request.getParameter("nid"));
		  int showHome = Integer.parseInt(request.getParameter("showHome"));
		  int result = nDaoImpl.newsUpdate(new NewsEntity(id, DateUtil.DateToDate(new Date()), newsTitle, newsContent, nid, showHome));
		  
		  PrintWriter out = response.getWriter(); 
		  out.print(Result.toClient("修改", result>0?true:false));
		  out.flush(); 
		  out.close(); 
	}
	
	public void newsAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  String newsTitle = request.getParameter("newsTitle");
		  String newsContent = request.getParameter("newsContent");
		  int nid = Integer.parseInt(request.getParameter("nid"));
		  int showHome = Integer.parseInt(request.getParameter("showHome"));
		  int result = nDaoImpl.newsAdd(new NewsEntity(0, DateUtil.DateToDate(new Date()), newsTitle, newsContent, nid, showHome));
		  
		  PrintWriter out = response.getWriter(); 
		  out.print(Result.toClient("添加", result>0?true:false));
		  out.flush(); 
		  out.close(); 
	}
	
	public void newsDelete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		int id= Integer.parseInt(request.getParameter("newsId"));
		int result=nDaoImpl.newsDelete(id);
		// 我们把数据输出到页面上
		PrintWriter printWriter = response.getWriter();
		printWriter.print(Result.toClient("删除",result>0?true:false));
		printWriter.flush();
		printWriter.close();
      }

}
